package info.touret.winecellar.userinfo.server.domain;

import info.touret.winecellar.dao.server.DataStoreObject;
import info.touret.winecellar.userinfo.shared.UserException;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.apache.log4j.Logger;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.annotation.Entity;

@Entity
public class UserInfo extends DataStoreObject<UserInfo> implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3000462501238066434L;

	@NotNull
	private String emailAddress;
	@Transient
	private String nickname;

	@Transient
	private User user;

	public User getUser() {
		if (user == null) {
			setUser(UserServiceFactory.getUserService().getCurrentUser());
		}
		return user;
	}

	private void setUser(User user) {
		this.user = user;
	}

	public boolean isLoggedIn() {
		return UserServiceFactory.getUserService().isUserLoggedIn();
	}

	public String getEmailAddress() {
		if (emailAddress == null) {
			setEmailAddress(getUser().getEmail());
		}
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	public String getNickname() {
		if (nickname == null) {
			setNickname(getUser().getNickname());
		}
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	
	public static String createLoginURL(String _toShow) {
		return UserServiceFactory.getUserService().createLoginURL(_toShow);
	}

	public static String createLogoutURL(String _toshow) {
		return UserServiceFactory.getUserService().createLogoutURL(_toshow);
	}

	public static List<UserInfo> findAllUserInfo() {
		Objectify objectify = ObjectifyService.begin();
		List<UserInfo> users = objectify.query(UserInfo.class).list();
		users.size();
		return users;
	}

	public static UserInfo findUserInfo(Long id) {
		Objectify objectify = ObjectifyService.begin();
		return objectify.find(UserInfo.class, id);
	}

	public static UserInfo findCurrentUserInApp(User user) throws UserException {
		Objectify objectify = ObjectifyService.begin();
		trace.info("user demande :"+user);
		UserInfo userinfo = objectify.query(UserInfo.class)
				.filter("emailAddress", user.getEmail()).get();
		if (userinfo == null) {
			// @todo modifier quand il y aura une console d administration /
			// voir import donnees
			UserInfo mockinfo = new UserInfo();
			mockinfo.setEmailAddress(user.getEmail());
			mockinfo.setUserID(Long.getLong(user.getUserId()));
			mockinfo.setId(1L);
			mockinfo.setUser(user);
			userinfo = mockinfo;
			// throw new UserException("L utilisateur ("+user+
			// ") n existe pas en base");
		}
		return userinfo;
	}

	private static final Logger trace = Logger.getLogger(UserInfo.class);

	/**
	 * @return
	 */
	public static Boolean isCurrentUserAuthorized() {
		Boolean isAuthorized = false;
		try {
			UserInfo userinfo = findCurrentUserInApp(UserServiceFactory
					.getUserService().getCurrentUser());
			if (userinfo != null) {
				isAuthorized = true;
			}
		} catch (UserException e) {
			trace.warn("Acces non autorise", e);
		}
		return isAuthorized;
	}
	
	private String gdatatoken;

	public String getGdatatoken() {
		return gdatatoken;
	}

	public void setGdatatoken(String token) {
		this.gdatatoken = token;
	}

	@Override
	public String toString() {
		return "UserInfo ["
				+ (emailAddress != null ? "emailAddress=" + emailAddress + ", "
						: "")
				+ (nickname != null ? "nickname=" + nickname + ", " : "")
				+ (user != null ? "user=" + user + ", " : "")
				+ (gdatatoken != null ? "gdatatoken=" + gdatatoken : "") + "]";
	}
	
	
	
}
